<template>
  <div>
    <el-page-header @back="goBack" content="新增用户" style="color:#3198ff"></el-page-header>
    <div style="margin-top: 30px;">
      <h4>基本信息</h4>
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户姓名" prop="consumername" :label-width="formLabelWidth">
              <el-input v-model="form.consumername"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职务" prop="duties" :label-width="formLabelWidth">
              <el-input v-model="form.duties"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="登录账号" prop="username" :label-width="formLabelWidth">
              <el-input v-model="form.username"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码" prop="password" :label-width="formLabelWidth">
              <el-input v-model="form.password" show-password></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" prop="description" :label-width="formLabelWidth">
          <el-input type="textarea" :rows="4" v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="店铺" prop="storeId" :label-width="formLabelWidth">
          <el-select v-model="form.storeId" placeholder="请选择">
            <el-option
              v-for="item in productOptions"
              :key="item.toreId"
              :label="item.name"
              :value="item.toreId"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item :label-width="formLabelWidth">
      <el-button plain @click="resetForm('formRef')">取消</el-button>
      <el-button type="primary" @click="submitForm('formRef')">确定</el-button>
        </el-form-item>-->
      </el-form>
      <h4>配置角色</h4>
      <div>
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="index" :index="indexMethod" label="序号" width="100"></el-table-column>
          <el-table-column prop="roleName" label="角色名称" width="300"></el-table-column>
          <el-table-column prop="remark" label="角色说明"></el-table-column>
          <el-table-column type="selection" width="80"></el-table-column>
        </el-table>
      </div>
      <div style="text-align:center; margin-top: 50px;">
        <el-button @click="backClick">取消</el-button>
        <el-button type="primary" @click="submitForm('formRef')">保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      productOptions:[],
      form: {
        duties: "",
        consumername: "",
        username: "",
        password: "",
        description: "",
        storeId: ""
      },
      tableData: [],
      multipleSelection: [],
      formLabelWidth: "120px",
      rules: {
        consumername: [
          { required: true, message: "请输入用户姓名", trigger: "blur" }
        ],
        username: [
          { required: true, message: "请输入登陆账号", trigger: "blur" },
          { min: 4, max: 15, message: "长度在 4 到 15 个字符", trigger: "blur" }
        ],
        duties: [{ required: true, message: "请输入职务", trigger: "blur" }],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^[^\u4e00-\u9fa5]+$/,
            message: "不允许输入中文",
            trigger: "blur"
          }
        ]
      }
    };
  },
  mounted() {
    this.$service.getRoleList().then(res => {
      //console.log(res);
      this.tableData = res.data;
    });
    this.getStoreList();
  },
  methods: {
    getStoreList() {
      this.$service.userGetStoreList().then(res => {
        if (res.code == "00000") {
          this.productOptions = res.data || [];
        }
      });
    },
    backClick() {
      this.$router.back();
    },
    goBack() {
      this.$router.back();
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        let message = this.form;
        if (valid) {
          if (this.multipleSelection.length == 0) {
            this.$message({
              message: "请至少选择一个角色"
            });
            return;
          }
          let param = {
            username: message.username,
            password: message.password,
            status: "1",
            roleId: this.multipleSelection.toString(),
            duties: message.duties,
            description: message.description,
            consumername: message.consumername,
            storeId:message.storeId
          };
          this.$service.userAdd(param).then(res => {
            if (res.code == "200") {
              this.$message({
                message: "添加成功",
                type: "success"
              });
              this.$emit("submit");
              this.$refs[formName].resetFields();
              this.$router.back();
            } else {
              this.$message({
                message: res.message,
                type: "warning"
              });
            }
          });
        }
      });
    },
    indexMethod(index) {
      var index1 = index + 1;
      return index1++;
    },
    handleSelectionChange(val) {
      console.log(val);
      this.multipleSelection = [];
      for (let i = 0; i < val.length; i++) {
        const element = val[i];
        this.multipleSelection.push(element.roleId);
      }
    },
    resetForm(formName) {
      this.$emit("submit", false);
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>